package com.white.commonlib.utils;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;

/**
 * Created by ytf on 2016/08/10.
 * Description:
 * Modify by xx on 2016/08/10.
 * Modify detail:
 */
public class DrawableUtils
{

    /***
     * 动态创建一个圆角矩形的shape
     * @param radiusPx 圆角半径
     * @param color    颜色
     * @return
     */
    public static GradientDrawable createShape(float radiusPx, int color)
    {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setCornerRadius(radiusPx);
        drawable.setColor(color);
        return drawable;
    }

    /***
     * 创建一个圆角矩形，可自定义边角的半径
     * @param leftTopRadius         左上角半径
     * @param rightTopRadius        右上角半径
     * @param leftBottomRadius      左下角半径
     * @param rightBottomRadius     右下角半径
     * @param color                  颜色
     * @return
     */
    public static GradientDrawable createRoundRectShape(float leftTopRadius, float rightTopRadius,
                                                        float leftBottomRadius, float rightBottomRadius, int color)
    {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE);
        drawable.mutate();
        drawable.setCornerRadii(new float[]{leftTopRadius,     leftTopRadius,
                                             rightTopRadius,    rightTopRadius,
                                             rightBottomRadius, rightBottomRadius,
                                             leftBottomRadius,  leftBottomRadius});
        drawable.setColor(color);
        return drawable;
    }

    /***
     * 动态创建创建选择器
     * @param activeState   激活是的drawable
     * @param normalState   未激活是的drawable
     * @return
     */
    public static StateListDrawable createSelectorDrawable(Drawable activeState, Drawable normalState){
        StateListDrawable stateListDrawable = new StateListDrawable();
        stateListDrawable.addState(new int[]{android.R.attr.state_checked}, activeState);
        stateListDrawable.addState(new int[]{}, normalState);
        return stateListDrawable;
    }

    /***
     * 动态创建颜色选择器
     * @param activeColor
     * @param normalColor
     * @return
     */
    public static ColorStateList createColorStateList(int activeColor, int normalColor){
        int[][] states = new int[2][];
        states[0] = new int[]{android.R.attr.state_checked};
        states[1] = new int[]{};
        return new ColorStateList(states, new int[]{activeColor, normalColor});
    }

    /***
     * 从资源创建颜色选择器
     * @param context
     * @param resId
     * @return
     */
    public static ColorStateList createColorStateList(Context context, int resId){
        return context.getResources().getColorStateList(resId);
    }

    /***
     * 从资源创建选择器
     * @param context
     * @param resId
     * @return
     */
    public static StateListDrawable createSelectorDrawable(Context context, int resId){
        return  (StateListDrawable) context.getResources().getDrawable(resId);
    }

}
